#!/usr/local/bin/php -q
<?PHP

include("/home/idealan/public_html/office/config.php");

//****************** EMAIL SETTINGS ***********************//
$mail_template = file_get_contents("mail.dat"); 
$find = array("[TITLE_COLOR]","[SERVER_STATUS]","[SERVER_NAME]","[SERVER_IP]","[LAST_CHECK]","[NOW]");

require_once ("/home/idealan/php/Mail.php");
/* mail setup recipients, subject etc */

$recipients = "lukasz@idealan.net";
$headers["MIME-Version"] = "1.0";
$headers["Content-Type"] = "text/html; charset=iso-8859-2";
$headers["From"] = "IdeaLAN Mailer <mailer@idealan.us>";
$headers["To"] = "Lukasz Piekarski <lukasz@idealan.net>";

/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "69.39.89.30";
$smtpinfo["port"] = "25";
$smtpinfo["auth"] = true;
$smtpinfo["username"] = "mailer@idealan.us";
$smtpinfo["password"] = "45t4k0616";

/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);

//****************** EMAIL SETTINGS ***********************//


// SERVER MONITOR AND TROUBLE REPORTER
$get_servers = "SELECT * FROM `".$DBprefix."servers`";

$execute_get_servers = mysql_query($get_servers); 
$count_servers = mysql_num_rows($execute_get_servers);

while ($server = mysql_fetch_array($execute_get_servers)) {
	
	$server_id = $server["server_id"];
	$server_name = $server["server_name"];
	$server_auth = $server["server_auth"];
	$server_ip = $server["server_ip"];
	$server_custid = $server["server_custid"];
	$server_upcount = $server["server_upcount"];
	$server_lastupdate = $server["server_lastupdate"];
		$last_check = date("d.m.Y H:i:s", $server_lastupdate);
		$now = date("d.m.Y H:i:s", $timedate);
	$server_notify = $server["server_notify"];
	$server_addby = $server["server_addby"];


	$server_green = $server_lastupdate + 600;
	$server_yellow = $server_lastupdate + 900;
	$server_red = $server_lastupdate + 1200;
		$last_checkR = date("H:i:s", $server_red);



	if ($timedate <= $server_green) {
		
		//$server_status = "<img src='http://office.idealan.us/g/ico/flag_green.png'>";

		if ($server_notify != "0") {
			
			$update_server = "UPDATE `".$DBprefix."servers` SET `server_notify` = '0' WHERE `server_id` = '$server_id'";  
			$result = mysql_query($update_server);

			$headers["Subject"] = "SERVER ONLINE";
			$title_color = "#0F0";
			$server_status = "ONLINE";
			$replace_with = array("$title_color","$server_status","$server_name","$server_ip","$last_check","$now"); 
			$message = str_replace($find,$replace_with, $mail_template);
			
			/* OK TO SEND EMAIL */
			$mail_object->send($recipients, $headers, $message);
			
		}

	} else if ($timedate >= $server_yellow AND $timedate <= $server_red) {
		
		//$server_status = "<img src='http://office.idealan.us/g/ico/flag_yellow.png'>";

		if ($server_notify == "0") {		
			
			$update_server = "UPDATE `".$DBprefix."servers` SET `server_notify` = '1' WHERE `server_id` = '$server_id'";  
			$result = mysql_query($update_server);
			
			$headers["Subject"] = "SERVER PROBLEMS";
			$title_color = "#C60";
			$server_status = "GOING DOWN";
			$replace_with = array("$title_color","$server_status","$server_name","$server_ip","$last_check","$now"); 
			$message = str_replace($find,$replace_with, $mail_template);

			/* OK TO SEND EMAIL */
			$mail_object->send($recipients, $headers, $message);

		}

	} else if ($timedate >= $server_red) {
		
		//$server_status = "<img src='http://office.idealan.us/g/ico/flag_red.png'>";
		
		if ($server_notify <= "1") {
		
			$update_server = "UPDATE `".$DBprefix."servers` SET `server_notify` = '2' WHERE `server_id` = '$server_id'";  
			$result = mysql_query($update_server);

			$headers["Subject"] = "SERVER DOWN";
			$title_color = "#C00";
			$server_status = "DOWN";
			$replace_with = array("$title_color","$server_status","$server_name","$server_ip","$last_check","$now"); 
			$message = str_replace($find,$replace_with, $mail_template);

			/* OK TO SEND EMAIL */
			$mail_object->send($recipients, $headers, $message);

		}

	}	
} 
// END SERVER MONITOR AND TROUBLE REPORTER

?>
